package com.chms.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.chms.bean.LoginBean;
import com.chms.utility.ApplicationConstant;
import com.chms.utility.ApplicationException;
import com.chms.utility.ExceptionMessage;
import com.chms.utility.PdfGenerator;
import com.chms.utility.StringUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFController extends BaseController {

	private static final Logger logger = Logger
			.getLogger(LoginController.class);

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.info("PDF controller called");
		
		String custName = request
				.getParameter(ApplicationConstant.custName);
		String custAddress = request
				.getParameter(ApplicationConstant.custAddress);
		String custMobile = request
				.getParameter(ApplicationConstant.custMobile);
		String emailAddress = request
				.getParameter(ApplicationConstant.emailAddress);
		String productTotalAmount = request
				.getParameter(ApplicationConstant.TOTAL_AMOUNT);
		String invoiceNumber = request
				.getParameter(ApplicationConstant.INVOICE_NO);
		String finalPrice = request
				.getParameter(ApplicationConstant.FINAL_AMOUNT);
		String courierCharges = request
				.getParameter(ApplicationConstant.courierCharges);
		String discount = request
				.getParameter(ApplicationConstant.discount);
		String serviceTax = request
				.getParameter(ApplicationConstant.SERIVCE_TAX);
		String orderInfo = request
				.getParameter(ApplicationConstant.CUST_PROD_INFO);
		String refNo = request
				.getParameter(ApplicationConstant.REF_NO);
		String custPayMode = request
				.getParameter(ApplicationConstant.custPayMode);
		
		Map<String,String> custValueMap = new HashMap<String, String>();
		custValueMap.put(ApplicationConstant.custName, custName);
		custValueMap.put(ApplicationConstant.custAddress, custAddress);
		custValueMap.put(ApplicationConstant.custMobile, custMobile);
		custValueMap.put(ApplicationConstant.custPayMode, custPayMode);
		custValueMap.put(ApplicationConstant.REF_NO, refNo);
		custValueMap.put(ApplicationConstant.emailAddress, emailAddress);
		custValueMap.put(ApplicationConstant.TOTAL_AMOUNT, productTotalAmount);
		custValueMap.put(ApplicationConstant.INVOICE_NO, invoiceNumber);
		custValueMap.put(ApplicationConstant.FINAL_AMOUNT, finalPrice);
		custValueMap.put(ApplicationConstant.courierCharges, courierCharges);
		custValueMap.put(ApplicationConstant.discount, discount);
		custValueMap.put(ApplicationConstant.SERIVCE_TAX, serviceTax);
			
		Map<String, List<String>> orderValueMap = new HashMap<String, List<String>>();
		try{
			JSONArray jArray = new JSONArray(orderInfo);
			List ordervalueList = null;
			for (int counter = 0; counter < jArray.length(); counter++) {
				JSONObject object = jArray.getJSONObject(counter);
				ordervalueList = new ArrayList<String>();

				String prodCode = "" + object.get("prodCode");
				String[] newCode = prodCode.split("C");
				if (StringUtil.isNullOrEmpty(prodCode))
					ordervalueList.add(newCode[1]);
				
				String prodName = "" + object.get("prodName");
				if (StringUtil.isNullOrEmpty(prodName))
					ordervalueList.add(prodName);

				String prodQuantity = "" + object.get("quantity");
				if (StringUtil.isNullOrEmpty(prodQuantity))
					ordervalueList.add(prodQuantity);

				String prodCoupon = "" + object.get("couponCode");
				if (StringUtil.isNullOrEmpty(prodCoupon))
					ordervalueList.add(prodCoupon);
				
				String prodPrice = "" + object.get("prodPrice");
				if (StringUtil.isNullOrEmpty(prodPrice))
					ordervalueList.add(prodPrice);
				
				String prodTotal = "" + object.get("amount");
				if (StringUtil.isNullOrEmpty(prodTotal))
					ordervalueList.add(prodTotal);
				
				orderValueMap.put(ApplicationConstant.ORDER + counter,
						ordervalueList);
			}
		} 
		catch (JSONException e) {
			logger.error("OrderService : prepareOrderContentMap() ----> "
				+ e.getMessage());
			/*throw new ApplicationException(
				ExceptionMessage.ORDER_CONTENT_NOT_RETRIEVED);*/
		}

		PdfGenerator pdg=new PdfGenerator();
		OutputStream file = new FileOutputStream(new File("F:\\Test.pdf"));

		Document document = new Document(PageSize.A4);
		try {
			PdfWriter.getInstance(document, file);
			document.open();
			PdfGenerator.addContent(document,custValueMap,orderValueMap);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		document.close();
		file.close();

	}
}
